		title    "Usart" ;*************************************
;*															  *
;* USART_INIT : Initiatization of USART communication port.   *
;* TXbyte     : send the WREG to the PC via the UART.         *
;* TXbstr     : send astring to the PC via the UART.          *
;* RXbyte     : waits for a byte to be received in UART.      *
;* TXcrlf     : Transmit  <CR> & <LF>  via the UART.	      *
;* TXspace    : Transmit  <Space> ' '  via the UART.          *
;**************************************************************
;
        list     P=PIC18F452
        #include p18F452.inc
;
    EXTERN   stan_table, ptr_pos
 	GLOBAL	 USART_INIT, TXbyte, RXbyte, TXstr, TXcrlf, TXspace, TXstr_1
;
;; Baudrate   0x19 = baud generator: 9600 baud, with Fosc - 4MHz. 
;
USAR   UDATA
Save   res 1
;
USART  CODE
;****************************
USART_INIT    ; initialize  *
; - - - - - - - - - - - - - *
;
	    bcf     TRISC,RC6        ; TX --> out   
		bsf     TRISC,RC7  	     ; RX <-- in 
		movlw   0x19			 ;9600 baud
		movwf   SPBRG            ;set baud rate
        bsf     TXSTA,BRGH       ;baud rate high speed option
        bsf     TXSTA,TXEN       ;enable transmission
        bsf     RCSTA,CREN       ;enable reception
        bsf     RCSTA,SPEN       ;enable serial port
        return
;
;************************
TXbyte     ; Transmit   *
; - - - - - - - - - - - *
; waits for the completion of the last transfer.
; Then it takes whatever is in W and sends it to the PC via the UART.
;
TXb
       ;Wait UNTIL  PIR1,TXIF == 1   ;for previous transfer to be completed
        btfss   PIR1,TXIF
        bra	    TXb
        movwf   TXREG            ;Transmit byte
        return
;
;************************
TXstr     ; Transmit   *
; - - - - - - - - - - - *
; display the string that is pointed to by TBLPTR and
; send it to the PC via the UART.
;
       movlw   UPPER  stan_table
       movwf   TBLPTRU
       movlw   HIGH  stan_table
       movwf   TBLPTRH
       movlw   LOW  stan_table
       movwf   TBLPTRL
       movf    ptr_pos,W
       addwf   TBLPTRL,F
       clrf    WREG
       addwfc  TBLPTRH,F
       addwfc  TBLPTRU,F
next_char
       tblrd*+
       movf     TABLAT,W
       bz       Rt
       call     TXbyte
       bra      next_char
Rt     return



;************************
TXstr_1     ; Transmit  *
; - - - - - - - - - - - *
; display the string that is pointed to by TBLPTR and
; send it to the PC via the UART.
;
	;       movlw   UPPER  stan_table
	;       movwf   TBLPTRU
	;       movlw   HIGH  stan_table
	;       movwf   TBLPTRH
	;       movlw   LOW  stan_table
	;       movwf   TBLPTRL
	;       movf    ptr_pos,W
	;       addwf   TBLPTRL,F
	;       clrf    WREG
	;       addwfc  TBLPTRH,F
	;       addwfc  TBLPTRU,F
next_char_1
       tblrd*+
       movf     TABLAT,W
       bz       Rt
       call     TXbyte
       bra      next_char_1
Rt_1   return



;
;************************
RXbyte     ; Receive    *
; - - - - - - - - - - - *
; waits for a byte to be received in UART.
; It returns with the byte in WREG.
;
        btfss   RCSTA,OERR       ;If an overrun has occurred
        bra	    RXb
        bcf     RCSTA, CREN      ; then clear it
        bsf     RCSTA, CREN      ; and reenable receives
RXb          
        btfss   PIR1,RCIF        ;Wait for byte to be received
        bra	    RXb
        movf    RCREG,W          ;Put received byte into W
	    return                   ; End  Delay subroutine.
;
;************************************
TXcrlf     ; Transmit  <CR> & <LF>  *
; - - - - - - - - - - - - - - - - - *
; waits for the completion of the last transfer.
; Then it takes whatever is in W and sends it to the PC via the UART.
;
	    movwf   Save
	    movlw   0x0D	 ; Carriage Return
TXcr
       ;Wait UNTIL  PIR1,TXIF == 1   ;for previous transfer to be completed
        btfss   PIR1,TXIF
        bra	    TXcr
        movwf   TXREG    ;Transmit byte
;
	    movlw   0x0A     ; Line Feed
TXlf
       ;Wait UNTIL  PIR1,TXIF == 1   ;for previous transfer to be completed
        btfss   PIR1,TXIF
        bra	    TXlf
        movwf   TXREG    ;Transmit byte
	    movf    Save,0
        return
;
;*************************************
TXspace     ; Transmit  <Space> ' '  *
; - - - - - - - - - - - - - - - - - -*
; waits for the completion of the last transfer.
; Then it takes the number which is in WREG and sends, via the UART,
; the amount of SPACE's as WREG has.
;
	    movwf   Save
	    movlw   A' '	 ; Space
TXsp
       ;Wait UNTIL  PIR1,TXIF == 1   ;for previous transfer to be completed
        btfss   PIR1,TXIF
        bra	    TXsp
        movwf   TXREG    ;Transmit byte
	    decfsz  Save,1
	    bra     TXsp
	    return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    end					; End  of the Program
